package com.xyz.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyz.entity.R;
import com.xyz.eduservice.entity.EduQuestion;
import com.xyz.eduservice.service.EduQuestionService;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 题库 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2021-06-15
 */
@Schema(description="题库管理接口")
@RestController
@RequestMapping("/eduservice/question")
@CrossOrigin
public class EduQuestionController {

    @Autowired
    private EduQuestionService questionService;

    @Schema(description = "添加题目")
    @PostMapping("save")
    public R save(@RequestBody EduQuestion eduQuestion){
        boolean save = questionService.save(eduQuestion);
        return save?R.ok():R.error();
    }

    @Schema(description = "删除题目")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id){
        boolean remove = questionService.removeById(id);
        return remove?R.ok():R.error();
    }

/**
 * Schema annotation to describe the API endpoint for updating a question
 * This endpoint handles HTTP POST requests to update an existing question
 */
    @Schema(description = "更新题目")
    @PostMapping("update")
/**
 * Method to update a question record in the database
 * @param eduQuestion The question object containing updated information, passed in the request body
 * @return Returns a success response (R.ok()) if update is successful,
 *         or an error response (R.error()) if update fails
 */
    public R update(@RequestBody EduQuestion eduQuestion){
    // Call the service layer to update the question by ID
        boolean update = questionService.updateById(eduQuestion);
    // Ternary operator to return appropriate response based on update operation result
        return update?R.ok():R.error();
    }

    @Schema(description = "根据ID获取题目")
    @GetMapping("get/{id}")
    public R get(@PathVariable String id){
        EduQuestion question = questionService.getById(id);
        return R.ok().data("item", question);
    }

    @Schema(description = "分页题目列表")
    @GetMapping("pageList/{page}/{limit}")
    public R pageList(@PathVariable Long page, @PathVariable Long limit,
                      @RequestParam(required = false) String courseId,
                      @RequestParam(required = false) String chapterId,
                      @RequestParam(required = false) String questionType,
                      @RequestParam(required = false) String title){
        Page<EduQuestion> pageParam = new Page<>(page, limit);

        QueryWrapper<EduQuestion> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseId)) {
            wrapper.eq("course_id", courseId);
        }
        if (!StringUtils.isEmpty(chapterId)) {
            wrapper.eq("chapter_id", chapterId);
        }
        if (!StringUtils.isEmpty(questionType)) {
            wrapper.eq("question_type", questionType);
        }
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        wrapper.orderByDesc("gmt_create");

        questionService.page(pageParam, wrapper);

        List<EduQuestion> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total", total).data("rows", records);
    }
    
    // 添加前端需要的接口方法
    @Schema(description = "分页题目列表(条件查询)")
    @GetMapping("pageListQuestionCondition/{page}/{limit}")
    public R pageListQuestionCondition(@PathVariable Long page, @PathVariable Long limit,
                                       @RequestParam(required = false) String courseId,
                                       @RequestParam(required = false) String chapterId,
                                       @RequestParam(required = false) String questionType,
                                       @RequestParam(required = false) String title) {
        return pageList(page, limit, courseId, chapterId, questionType, title);
    }
}